function checkTB(sid) {
    var tbNum = new Array(1, 2, 1, 2, 1, 2, 4, 1);
    var temp = 0;
    var total = 0;
    //alert(sid.length);
    if (sid == "")
        return false;//alert("請先輸入欲檢驗的營利事業統一編號!");
    else if (!sid.match(/^\d{8}$/))
        return false;//alert("營利事業統一編號長度不夠或格式有誤!請注意營利事業統一編號格式為八碼數字!");
    else {
        for (var i = 0; i < tbNum.length; i++) {
            temp = sid.charAt(i) * tbNum[i];
            total += Math.floor(temp / 10) + temp % 10;
        }
        if (total % 10 == 0 || (total % 10 == 9 && sid.charAt(6) == 7))
            return true; //alert("營利事業統一編號正確!");
        else
            return false; //alert("營利事業統一編號錯誤!");
    }
}





$("document").ready(function() {



    $( "#home_building_name" )
      .bind( 'focus', function(){           
            $(this).autocomplete("search"); 
      } )
      .bind( 'click', function(){                   
            $(this).autocomplete("search"); 
      } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/building_name_josn",
                    dataType: "json",
                    data:{
                        query: request.term
                    },
                    success: function( data ) {

                        response( $.map( data, function( item ) {
                        return {
                            label: item.value,
                            value: item.value
                        }
                            return false;
                        }));
                    //alert('aaakaki');
                    return false;
                    }
                });
        },
        focus: function() {
          return false;
        }
      });



$( "#home_building_name1" )
      .bind( 'focus', function(){ 

        var b_name1=$( "#home_building_name1" ).val();
        if(b_name1.length==0)
            $(this).autocomplete("search"); 

      } )
      .bind( 'click', function(){ 

        var b_name1=$( "#home_building_name1" ).val();
        //if(b_name1.length==0)
            $(this).autocomplete("search"); 

      } )
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/building_name_josn",
                    dataType: "json",
                    data:{
                        query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
                        label: item.value,
                        value: item.value
                        }
                        }));
                    }
                });
        },
        focus: function() {
          return false;
        }
});


$( "#home_building_name2" )
      .bind( 'focus', function(){ 

        var b_name2=$( "#home_building_name2" ).val();
        if(b_name2.length==0)
            $(this).autocomplete("search"); 

      } )
      .bind( 'click', function(){ 

        var b_name2=$( "#home_building_name2" ).val();
        //if(b_name2.length==0)
            $(this).autocomplete("search"); 

      })
      .autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/building_name_josn",
                    dataType: "json",
                    data:{
                        query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
                        label: item.value,
                        value: item.value
                        }
                        }));
                        return false;
                    }
                });
        },
        focus: function() {
          return false;
        }
});

    $("#unicode").blur(function() {

        var re_flag = checkTB($(this).val());

        if (re_flag) {
            
        } else {
            $(this).val("");
            alert("營利事業統一編號長度不夠或格式有誤!請注意營利事業統一編號格式為八碼數字!");
        }

    });
    
    //制作分頁
    $("a").click(function() {

        var tag_class = $(this).attr("class");

        //alert('www');
        if (tag_class == "client") {
            $("#contact_info").hide();
            $("#relationship_info").hide();
            $("#client_info").show();
        }

        if (tag_class == "contact") {
            $("#contact_info").show();
            $("#relationship_info").hide();
            $("#client_info").hide();
        }

        if (tag_class == "relationship") {
            $("#contact_info").hide();
            $("#relationship_info").show();
            $("#client_info").hide();
        }


    });





    /*
     //CLIENT人寫到資料庫//
     $("#client_submit").click(function(){
     
     
     //關西人寫到資料庫//
     $("#contact_info").hide();
     $("#relationship_info").show();
     $("#client_info").hide();
     
     alert(
     $("from#relationship_info_form").serialize() 
     );
     
     $.ajax({
     type: "POST",
     url:'/show/relationship/insert_relationship_submit',
     data: $("from#relationship_info_form").serialize(),
     success: function(msg) {
     alert(msg);                                                                   
     }
     });
     
     
     
     $("#contact_info").hide();
     $("#relationship_info").hide();
     $("#client_info").show();
     
     
     $.ajax({
     type: "POST",
     url:'/client/data_entry_submit',
     data: $("#userinfo").serialize(),
     success: function(msg) {
     //alert(msg);                                                                   
     }
     });
     
     
     
     
     //聯絡人寫到資料庫//
     //$("#contact_submit").click(function(){
     
     $("#contact_info").show();
     $("#relationship_info").hide();
     $("#client_info").hide();
     
     $.ajax({
     type: "POST",
     url:'/show/contact/insert_contact_submit',
     data: $("form#contact_info").serialize(),
     success: function(msg) {
     //alert(msg);                                                                   
     }
     });
     
     location.href = "/client/data_query_results";
     
     });
     */


    //編輯～關係人～聯絡人
    /*
     $("#update_client").click(function(){
     
     $("form").each(function(){
     //～聯絡人
     if($(this).attr("id")=="contact"){
     
     $.ajax({
     type: "POST",
     url:'/show/contact/update_contact_submit',
     data: $(this).serialize(),
     success: function(msg) {
     //alert(msg);                                                                   
     }
     });
     }                  
     
     
     
     //～關係人
     if($(this).attr("id")=="relationship_form"){
     
     //alert($(this).html());    
     $(this).html(function(){
     
     //  alert($("input").attr("name"));
     
     });                
     $.ajax({
     type: "POST",
     url:'/show/relationship/update_relationship_submit',
     data: $(this).serialize(),
     success: function(msg) {
     //alert(msg);                                                                   
     }
     });
     }
     
     
     });
     
     alert('qqq');
     
     
     
     });
     
     
     //新增客戶－關係人－聯絡人
     $("#client_submit_form").click(function() {
     
     
     alert($("#client_userinfo").serialize());
     $.ajax({
     type: "POST",
     url: '/client/data_entry_submit',
     data: $("#client_userinfo").serialize(),
     success: function(msg) {
     alert(msg);
     }
     });
     
     
     alert($("#contact_info").serialize());
     
     //新增客戶－聯絡人
     $.ajax({
     type: "POST",
     url: '/client/data_entry_contact_submit',
     data: $("#contact_info").serialize(), // 要提交的表单
     success: function(msg) {
     //alert(msg);                                                                   
     }
     });
     
     $("#tabs-1").html("");
     $("#tabs-3").html("");
     $("#client_userinfo").html("");
     $("#detail_info").html("");
     //新增客戶－關係人1
     $.ajax({
     type: "POST",
     url: '/client/data_entry_relationship_submit2',
     data: {
     "name": $("#rela_name").val(),
     "english_name": $("#rela_english_name").val(),
     "ref_client_verify_code": $(".ref_client_verify_code").val(),
     "sex": $("#rela_sex").val(),
     "mobile": $("#rela_mobile").val(),
     "home_phone": $("#rela_home_phone").val(),
     "company_phone": $("#rela_company_phone").val(),
     "fax": $("#rela_fax").val(),
     "home_address": $("#rela_home_address").val(),
     "company_address": $("#rela_company_address").val()
     
     }, // 要提交的表单
     success: function(msg) {
     
     
     }
     
     });
     
     
     
     //新增客戶－之後    
     $("form").each(function() {
     
     $.ajax({
     type: "POST",
     url: '/client/data_entry_relationship_submit',
     data: $(this).serialize(), // 要提交的表单
     success: function(msg) {
     //alert(msg);
     }
     
     });
     
     });
     document.location.href = "/client/data_query_results";
     
     });
     */


    $("#mass_edm").click(function() {

        $("#userinfo").attr("action", "/send_edm/send_edm_group").submit();

    });

    $("#client_reset").click(function() {

        $(":input").each(function() {
            if ($(this).attr("type") != "button" &&
                    $(this).attr("type") != "radio" &&
                    $(this).attr("type") != "submit") {

                $(this).val("");
            }
        });


        $(":radio").each(function() {
            //$(this).val("Y");
        });


        $("#send_sms[value='Y']").attr('checked', true);
        $("#send_sms1[value='Y']").attr('checked', true);
        $("#send_sms2[value='Y']").attr('checked', true);
        //send_email
        $("#send_email[value='Y']").attr('checked', true);
        $("#send_email1[value='Y']").attr('checked', true);
        $("#send_email2[value='Y']").attr('checked', true);
        //home_send_publication2
        $("#gifts[value='N']").attr('checked', true);

        $("#company_send_publication[value='Y']").attr('checked', true);
        $("#company_send_publication1[value='Y']").attr('checked', true);
        $("#company_send_publication2[value='Y']").attr('checked', true);

        $("#home_send_publication[value='Y']").attr('checked', true);
        $("#home_send_publication1[value='Y']").attr('checked', true);
        $("#home_send_publication2[value='Y']").attr('checked', true);
        //nationality
        $("#sex[value='1']").attr('checked', true);

        $("#nationality").val(1);


        $("select").each(function() {
            $(this).val("");
        });

        $("#nationality").val(1);

        $("#type_of_company").val("");
        $("#type_of_company1").val("");
        $("#type_of_company2").val("");

    });




    $("#turn_excel_member").click(function() {

        var values = $('#client_check_box:checked').map(function() {
            return this.value;
        }).get();

        $("form#userinfo").attr("action", "/client/excel");
        $("form#userinfo").submit();

    });


    $("#turn_excel_member_victor").click(function() {

        $("form#userinfo").attr("action", "/client/excel_ckecked");
        $("form#userinfo").submit();
        //location.reload();

    });



    $(":checkbox").each(function() {
        $(this).click(function() {

            if ($(this).attr('checked')) {

                $.post("/client/save_client_id", {id: $(this).val(), "is_del": 0},
                function(result) {

                });

            } else {

                $.post("/client/save_client_id", {id: $(this).val(), "is_del": 1},
                function(result) {

                });
            }

        });
    });





});



//增加關係人
function add_relationship() {


    var v = $("table#contact_count");
    var i = v.length;
    var ref_client_verify_code = $(".ref_client_verify_code").val();
  

    $.post("/show/relationship/show_html",
            {
                count: i,
                ref_client_verify_code: ref_client_verify_code
            },
    function(data) {
        $("div#html_relationship").append(data);
                });


}

//增加聯絡人
function add_contact() {


    var v = $("table#contact_count");
    var i = v.length;

    $.post("/show/contact/show_html",
            {
                count: i

            },
    function(data) {
        $("div#html_contact").append(data);
        //alert(data);
                });


}

function add_mobile() {
    var v = $("input[name^='mobile']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {
        $("#add_tel").append("<li id='mobile_" + i + "'><input type='text' style='width:260px;'  name='mobile" + i + "' id='mobile" + i + "' class='mobile' /> <a onclick='remove_mobile(" + i + ")' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
        $("#mobilesms").append("<li id='mobile_" + i + "'><input type='radio'  name='send_sms" + i + "' id='send_sms" + i + "' value='Y' checked/>Y&nbsp;<input type='radio' name='send_sms" + i + "' id='send_sms" + i + "' value='N'/>N</li>");
    }
}
function remove_mobile(item) {
    $("#mobile_" + item).remove();
    $("#mobile_" + item).remove();
}

function add_home_phone() {
    var v = $("input[name^='home_phone']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {
        $("#home_tel").append("<li id='home_phone_" + i + "'><input type='text' class='colspan_input1' style='width:260px;' name='home_phone" + i + "' id='home_phone" + i + "'/><a onclick='remove_home_phone(" + i + ")' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
    }
}
function remove_home_phone(item) {
    $("#home_phone_" + item).remove();
}

function add_company_phone() {
    var v = $("input[name^='company_phone_ext']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {
        $("#company_tel").append("<li id='company_phone_" + i + "'><input type='text' name='company_phone" + i + "' id='company_phone" + i + "' class='colspan_input1' style='width:260px;' />&nbsp;&nbsp;<b>分機</b>&nbsp;&nbsp;<input type='text' class='c_tel_2' name='company_phone_ext" + i + "' id='company_phone_ext" + i + "' ><a onclick='remove_company_phone(" + i + ")' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
    }
}
function remove_company_phone(item) {
    $("#company_phone_" + item).remove();
}

function add_email() {
    var v = $("input[name^='email']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {
        var radio = "";
        $("#email0").append("<li id='email_" + i + "'><input type='text' name='email" + i + "' id='email" + i + "' class='email_input' onblur=checkemail('email" + i + "') /><input type='button' value='開啟mail' onclick=open_email('email" + i + "') class='input_background'/><a onclick='remove_email(" + i + ")' class='deleteitem' ><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
        $("#email_edm").append("<li id='send_email_" + i + "' class='7'><input type='radio' name='send_email" + i + "' id='send_email" + i + "' value='Y' checked='checked' />Y&nbsp;&nbsp;<input type='radio' name='send_email" + i + "' id='send_email" + i + "' value='N'/>N</li>");
        setTimeout(function() {
            $("#email" + i).focus();
        }, 0);
    }
}
function remove_email(item) {
    $("#email_" + item).remove();
    $("#send_email_" + item).remove();
}

function add_fax() {
    var v = $("input[name^='fax']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {
        $("#fax_u").append("<li id='fax_" + i + "'><input type='text' name='fax" + i + "' id='fax" + i + "' class='colspan_input1' style='width:260px;' /><a onclick='remove_fax(" + i + ")' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
    }
}
function remove_fax(item) {
    $("#fax_" + item).remove();
}

function add_home_address() {
    var v = $("input[name^='home_address']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {

        var build_name = $("#home_building_name").html();
        $("#home_addr").append("<li id='home_address_" + i + "'><input type='text' class='colspan_input' name='home_address" + i + "' id='home_address" + i + "' onclick=show_address('home_address" + i + "') /><a onclick='remove_home_address(" + i + ")' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
        $("#building_name").append("<li id='home_building_name_" + i + "' ><input id='home_building_name" + i + "' name='home_building_name" + i + "' size='50' style='width:148px;'  /></li>");
        $("#per").append("<li id='home_send_publication_" + i + "' ><input type='radio' name='home_send_publication" + i + "' id='home_send_publication" + i + "' value='Y' checked/>Y&nbsp;&nbsp;<input type='radio' name='home_send_publication" + i + "' id='home_send_publication" + i + "' value='N'/>N</li>");
        $("#home_building_name1")
                .bind('focus', function() {

                    var b_name1 = $("#home_building_name1").val();
                    if (b_name1.length == 0)
                        $(this).autocomplete("search");

                }).bind('click', function() {

            var b_name1 = $("#home_building_name1").val();
            if (b_name1.length == 0)
                $(this).autocomplete("search");

        })
                .autocomplete({
                    minLength: 0,
                    source: function(request, response) {
                                        $.ajax({
                                                url: "/client/building_name_josn",
                                                dataType: "json",
                                                data: {
                                query: request.term
                                                },
                                                success: function(data) {
                                                        response($.map(data, function(item) {
                                                            return {
                                        label: item.value,
                                        value: item.value
                                                            }
                                                        }));
                                                }
                                        });
                        },
                    focus: function() {
                        return false;
                    }
                });


        $("#home_building_name2")
                .bind('focus', function() {

                    var b_name2 = $("#home_building_name2").val();
                    if (b_name2.length == 0)
                        $(this).autocomplete("search");

                }).bind('click', function() {

            var b_name2 = $("#home_building_name2").val();
            if (b_name2.length == 0)
                $(this).autocomplete("search");

        })
                .autocomplete({
                    minLength: 0,
                    source: function(request, response) {
                                        $.ajax({
                                                url: "/client/building_name_josn",
                                                dataType: "json",
                                                data: {
                                query: request.term
                                                },
                                                success: function(data) {
                                                        response($.map(data, function(item) {
                                                            return {
                                        label: item.value,
                                        value: item.value
                                                            }
                                                        }));
                                                }
                                        });
                        },
                    focus: function() {
                        return false;
                    }
                });

        /*
         $("#home_building_name" + i)
         .bind('focus', function() {
         var t_v= $(this).val();
         
         if(t_v.length==0){
         alert(t_v);
         $(this).autocomplete("search");
         }
         }).bind('click', function() {
         $(this).autocomplete("search");
         }).autocomplete({
         
         minLength: 0,
         source: function(request, response) {
                         $.ajax({
                             url: "/client/building_name_josn",
                             dataType: "json",
                             data: {
         query: request.term
                             },
                             success: function(data) {
                                 response($.map(data, function(item) {
                                 return {
         label: item.value,
         value: item.value
                                 }
                                 }));
                             }
                         });
             },
         focus: function() {
         return false;
         }
         });
         */
    }
}



function remove_home_address(item) {
    $("#home_address_" + item).remove();
    $("#home_building_name_" + item).remove();
    $("#home_send_publication_" + item).remove();
}
function add_usually_contact() {
    var v = $("select[name^='usually_contact']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {
        var usually_contact = $("#usually_contact").html();
        $("#ping").append("<li class='ping_li' id='usually_contact_" + i + "'><select style='width:70px' name='usually_contact" + i + "' id='usually_contact" + i + "'>" + usually_contact + "</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;起&nbsp;<input type='text' style='width:70px;' onfocus=\"WdatePicker({dateFmt:'HH:mm'})\" class='Wdate' id='usually_start_time" + i + "' name='usually_start_time" + i + "'>&nbsp;&nbsp;&nbsp;&nbsp;止&nbsp;<input type='text' style='width:70px;' onfocus=\"WdatePicker({dateFmt:'HH:mm'})\" class='Wdate' id='usually_end_time" + i + "' name='usually_end_time" + i + "'>&nbsp;<a class='deleteitem' onclick='remove_usually_contact(" + i + ")'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
    }
}

function remove_usually_contact(item) {
    $("#usually_contact_" + item).remove();
}

function add_holidays_contact() {
    
    var v = $("select[name^='holidays_contact']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {
        var holidays_contact = $("#holidays_contact").html();
        $("#holiday").append("<li class='holiday_li' id='holidays_contact_" + i + "'><select style='width:70px;' id='holidays_contact" + i + "' name='holidays_contact" + i + "'>" + holidays_contact + "</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;起&nbsp;<input type='text' style='width:70px;' onfocus=\"WdatePicker({dateFmt:'HH:mm'})\" class='Wdate' id='holidays_start_time" + i + "' name='holidays_start_time" + i + "'>&nbsp;&nbsp;&nbsp;&nbsp;止&nbsp;<input type='text' style='width:70px;' onfocus=\"WdatePicker({dateFmt:'HH:mm'})\" class='Wdate' id='holidays_end_time" + i + "' name='holidays_end_time" + i + "'>&nbsp;<a class='deleteitem' onclick='remove_holidays_contact(" + i + ")'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
    }
}

function remove_holidays_contact(item) {

    $("#holidays_contact_" + item).remove();
}

function add_company() {


    var v = $("input[name^='company_website']");
    var i = v.length;
    if (i > 2) {
        alert("最多只能添加3個哦");
        return;
    } else {


        $("#company_name0").append("<li id='company_name_" + i + "'><input type='text' class='colspan_input' id='company_name" + i + "'  name='company_name" + i + "' /><a onclick='remove_company(" + i + ")' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
        $.post("/client/type_of_company", {
            i: i
        },
        function(result) {
            if (result != "") {
                $('#company_type').append(result);
            }
        });



        $("#company_id").append("<li id='unicode_" +
                i +
                "' class='13'>" +
                "<input type='text' name='unicode" +
                i
                + "' onblur=checkunicode('unicode" +
                i
                + "') id='unicode" +
                i
                + "' style='width:160px;'/></li>");

        
        
        $("#unicode"+i).blur(function() {

            var re_flag = checkTB($(this).val());

            if (re_flag) {

            } else {
                $(this).val('');
                alert("營利事業統一編號長度不夠或格式有誤!請注意營利事業統一編號格式為八碼數字!");
            }

        });





        $("#dept").append("<li id='department_" + i + "' class='13'><input type='text' id='department" + i + "' name='department" + i + "' /></li>");
        $.post("/client/title", {
            i: i
        },
        function(result) {
            if (result != "") {
                $('#zhichen').append(result);
            }
        });


        $.post("/client/category", {
            i: i
        },
        function(result) {

            if (result != "") {
                $('#company_type2').append(result);

                $("#category" + i).click(function(){

     $(this).autocomplete("search"); 


}).autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/category_josn",
                    dataType: "json",
                    data:{
                        query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
                        label: item.value,
                        value: item.value
                        }
                        }));
                    }
          
                });
        },
        focus: function() {
            //alert('aaa');
          return false;
        }
        ,
        select: function( event, ui ) {
            var terms=new Array();
            if(this.value!="")
                terms = this.value.split(", ");
            terms.pop();
            terms.push( ui.item.value );
            terms.push( "" );
            this.value = terms.join( ", " );
            return false;
        }
      });

            }
        });


        $('#company_addr').append("<li id='company_address_" + i + "'><input type='text' id='company_address" + i + "' class='large_input' style='width:373px;' name='company_address" + i + "' onclick=show_address('company_address" + i + "')></li>");
        $.post("/client/company_building_name", {
            i: i
        },
        function(result) {
            //alert(result);
            if (result != "") {

                $('#building_company_name').append(result);

                $("#company_building_name" + i)
                        .bind('focus', function() {
                            $(this).autocomplete("search");
                        }).bind('click', function() {
                    $(this).autocomplete("search");
                }).autocomplete({
                    minLength: 0,
                    source: function(request, response) {
                                        $.ajax({
                                                url: "/client/building_name_josn",
                                                dataType: "json",
                                                data: {
                                query: request.term
                                                },
                                                success: function(data) {
                                                        response($.map(data, function(item) {
                                                            return {
                                        label: item.value,
                                        value: item.value
                                                            }
                                                        }));
                                                }
                                        });
                        },
                    focus: function() {
                        return false;
                    }
                });
            }
        });
        $('#per_com').append("<li id='company_send_publication_" + i + "'><input type='radio' checked value='Y' id='company_send_publication" + i + "' name='company_send_publication" + i + "'>Y&nbsp;&nbsp;<input type='radio' value='N' id='company_send_publication" + i + "' name='company_send_publication" + i + "'>N</li>");
        $('#company_website0').append("<li id='company_website_" + i + "'><input type='text' id='company_website" + i + "' style='width:374px;' name='company_website" + i + "'><input type='button' class='input_background' onclick=open_website('company_website" + i + "'); value='開啟IE'></li>");

        $("#company_name" + i).autocomplete({
                        source: function(request, response) {
                                $.ajax({
                                        url: "/client/company_name_josn",
                                        dataType: "json",
                                        data: {
                        query: request.term
                                        },
                                        success: function(data) {
                                                response($.map(data, function(item) {
                                                    return {
                                label: item.company_name,
                                value: item.company_name,
                                type_of_company: item.type_of_company,
                                unicode: item.unicode,
                                category: item.category,
                                company_address: item.company_address,
                                company_building_name: item.company_building_name,
                                company_send_publication: item.company_send_publication,
                                company_website: item.company_website

                                                    }
                                                }));
                                        }
                                });
                        },
            select: function(e, ui) {
                $("#type_of_company" + i).val(ui.item.type_of_company);
                $("#unicode" + i).val(ui.item.unicode);
                $("#category" + i).val(ui.item.category);
                $("#company_address" + i).val(ui.item.company_address);
                $("#company_building_name" + i).val(ui.item.company_building_name);
                $("#company_send_publication" + i).val(ui.item.company_send_publication);
                $("#company_website" + i).val(ui.item.company_website);





            },
                        minLength: 1
         	});
    }
}


function remove_company(item) {
    $("#company_name_" + item).remove();
    $("#type_of_company_" + item).remove();
    $("#unicode_" + item).remove();
    $("#department_" + item).remove();
    $("#title_" + item).remove();
    $("#category_" + item).remove();
    $("#company_address_" + item).remove();
    $("#company_building_name_" + item).remove();
    $("#company_send_publication_" + item).remove();
    $("#company_website_" + item).remove();
}

function query_more() {
    if ($("#query_more").css("display") == "none") {
        $("#query_more").show();
        $("#query_more_icon").removeClass("icon-plus");
        $("#query_more_icon").addClass("icon-minus");
    } else {
        $("#query_more").hide();
        $("#query_more_icon").removeClass("icon-minus");
        $("#query_more_icon").addClass("icon-plus");
    }
}

function selectCheckBox() {
    if ($("#select_client").attr("checked")) {
        $("[id='client_check_box']").attr("checked", 'true');
    } else {
        $("[id='client_check_box']").removeAttr("checked");
    }
}


function checkform() {
    if ($("#name").val() == "") {
        alert("請填寫客戶姓名");
        setTimeout(function() {
            $("#name").focus();
        }, 0);
        return false;
    } else if ($("#id_number").val() != "") {
        checkidnumber();
    } else {
        return true;
    }
}

function check_client_name() {
    if ($("#name").val() != "") {

        $.post("/client/client_name_josn", {
            name: $("#name").val()
        },
        function(result) {
            if (result != "N") {
                if (confirm('該客戶姓名已經存在，是否帶入資料？')) {
                    remove_mobile(1);
                    remove_mobile(2);
                    remove_home_phone(1);
                    remove_home_phone(2);
                    remove_company_phone(1);
                    remove_company_phone(2);
                    remove_email(1);
                    remove_email(2);
                    remove_fax(1);
                    remove_fax(2);
                    remove_home_address(1);
                    remove_home_address(2);
                    remove_usually_contact(1);
                    remove_usually_contact(2);
                    remove_holidays_contact(1);
                    remove_holidays_contact(2);
                    remove_company(1);
                    remove_company(2);
                    var v = eval(result);
                    $("#recommended").val(v.recommended);
                    $("#importance").val(v.importance);
                    $("#nationality").val(v.nationality);
                    $("#english_name").val(v.english_name);
                    $("input[type=radio][name=sex][value=" + v.sex + "]").attr("checked", "checked");
                    $("#customer_source").val(v.customer_source);
                    $("#mobile").val(v.mobile);
                    if (v.mobile1 != "" && v.mobile1 != "0") {
                        add_mobile();
                        $("#mobile1").val(v.mobile1);
                    }
                    if (v.mobile2 != "" && v.mobile2 != "0") {
                        add_mobile();
                        $("#mobile2").val(v.mobile2);
                    }
                    $("input[type=radio][name=send_sms][value=" + v.send_sms + "]").attr("checked", "checked");
                    if (v.send_sms1 != "" && v.send_sms1 != "0") {
                        $("input[type=radio][name=send_sms1][value=" + v.send_sms1 + "]").attr("checked", "checked");
                    }
                    if (v.send_sms2 != "" && v.send_sms2 != "0") {
                        $("input[type=radio][name=send_sms2][value=" + v.send_sms2 + "]").attr("checked", "checked");
                    }
                    $("#home_phone").val(v.home_phone);
                    if (v.home_phone1 != "" && v.home_phone1 != "0") {
                        add_home_phone();
                        $("#home_phone1").val(v.home_phone1);
                    }
                    if (v.home_phone2 != "" && v.home_phone2 != "0") {
                        add_home_phone();
                        $("#home_phone2").val(v.home_phone2);
                    }
                    $("#company_phone").val(v.company_phone);
                    if (v.company_phone1 != "" && v.company_phone1 != "0") {
                        add_company_phone();
                        $("#company_phone1").val(v.company_phone1);
                    }
                    if (v.company_phone2 != "" && v.company_phone2 != "0") {
                        add_company_phone();
                        $("#company_phone2").val(v.company_phone2);
                    }
                    $("#company_phone_ext").val(v.company_phone_ext);
                    if (v.company_phone_ext1 != "" && v.company_phone_ext1 != "0") {
                        $("#company_phone_ext1").val(v.company_phone_ext1);
                    }
                    if (v.company_phone_ext2 != "" && v.company_phone_ext2 != "0") {
                        $("#company_phone_ext2").val(v.company_phone_ext2);
                    }
                    $("#email").val(v.email);
                    if (v.email1 != "" && v.email1 != "0") {
                        add_email();
                        $("#email1").val(v.email1);
                    }
                    if (v.email2 != "" && v.email2 != "0") {
                        add_email();
                        $("#email2").val(v.email2);
                    }
                    $("input[type=radio][name=send_email][value=" + v.send_email + "]").attr("checked", "checked");
                    if (v.send_email1 != "" && v.send_email1 != "0") {
                        $("input[type=radio][name=send_email1][value=" + v.send_email1 + "]").attr("checked", "checked");
                    }
                    if (v.send_email2 != "" && v.send_email2 != "0") {
                        $("input[type=radio][name=send_email2][value=" + v.send_email2 + "]").attr("checked", "checked");
                    }
                    $("#fax").val(v.fax);
                    if (v.fax1 != "" && v.fax1 != "0") {
                        add_fax();
                        $("#fax1").val(v.fax1);
                    }
                    if (v.fax2 != "" && v.fax2 != "0") {
                        add_fax();
                        $("#fax2").val(v.fax2);
                    }
                    $("#home_address").val(v.home_address);
                    if (v.home_address1 != "" && v.home_address1 != "0") {
                        add_home_address();
                        $("#home_address1").val(v.home_address1);
                    }
                    if (v.home_address2 != "" && v.home_address2 != "0") {
                        add_home_address();
                        $("#home_address2").val(v.home_address2);
                    }
                    $("#home_building_name").val(v.home_building_name);
                    if (v.home_building_name1 != "" && v.home_building_name1 != "0") {
                        $("#home_building_name1").val(v.home_building_name1);
                    }
                    if (v.home_building_name2 != "" && v.home_building_name2 != "0") {
                        $("#home_building_name2").val(v.home_building_name2);
                    }
                    $("input[type=radio][name=home_send_publication][value=" + v.home_send_publication + "]").attr("checked", "checked");
                    if (v.home_send_publication1 != "" && v.home_send_publication1 != "0") {
                        $("input[type=radio][name=home_send_publication1][value=" + v.home_send_publication1 + "]").attr("checked", "checked");
                    }
                    if (v.home_send_publication2 != "" && v.home_send_publication2 != "0") {
                        $("input[type=radio][name=home_send_publication2][value=" + v.home_send_publication2 + "]").attr("checked", "checked");
                    }
                    $("#company_name").val(v.company_name);
                    if (v.company_name1 != "" && v.company_name1 != "0") {
                        $("#company_name0").append("<li id='company_name_1'><input type='text' class='colspan_input' id='company_name1'  name='company_name1' value='" + v.company_name1 + "' /><a onclick='remove_company(1)' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
                        $("#company_name1").autocomplete({
                                        source: function(request, response) {
                                                $.ajax({
                                                        url: "/client/company_name_josn",
                                                        dataType: "json",
                                                        data: {
                                        query: request.term
                                                        },
                                                        success: function(data) {
                                                                response($.map(data, function(item) {
                                                                    return {
                                                label: item.company_name,
                                                value: item.company_name,
                                                type_of_company: item.type_of_company,
                                                unicode: item.unicode,
                                                category: item.category,
                                                company_address: item.company_address,
                                                company_building_name: item.company_building_name,
                                                company_send_publication: item.company_send_publication,
                                                company_website: item.company_website

                                                                    }
                                                                }));
                                                        }
                                                });
                                        },
                            select: function(e, ui) {
                                $("#type_of_company1").val(ui.item.type_of_company);
                                $("#unicode1").val(ui.item.unicode);
                                $("#category1").val(ui.item.category);
                                $("#company_address1").val(ui.item.company_address);
                                $("#company_building_name1").val(ui.item.company_building_name);
                                $("input[type=radio][name=company_send_publication1][value=" + ui.item.company_send_publication + "]").attr("checked", "checked");
                                $("#company_website1").val(ui.item.company_website);
                            },
                                        minLength: 1
                         	});
                    }
                    if (v.company_name2 != "" && v.company_name2 != "0") {
                        $("#company_name0").append("<li id='company_name_2'><input type='text' class='colspan_input' id='company_name2'  name='company_name2' value='" + v.company_name2 + "' /><a onclick='remove_company(2)' class='deleteitem'><i class='icon-trash'>&nbsp;&nbsp;</i></a></li>");
                        $("#company_name2").autocomplete({
                                        source: function(request, response) {
                                                $.ajax({
                                                        url: "/client/company_name_josn",
                                                        dataType: "json",
                                                        data: {
                                        query: request.term
                                                        },
                                                        success: function(data) {
                                                                response($.map(data, function(item) {
                                                                    return {
                                                label: item.company_name,
                                                value: item.company_name,
                                                type_of_company: item.type_of_company,
                                                unicode: item.unicode,
                                                category: item.category,
                                                company_address: item.company_address,
                                                company_building_name: item.company_building_name,
                                                company_send_publication: item.company_send_publication,
                                                company_website: item.company_website

                                                                    }
                                                                }));
                                                        }
                                                });
                                        },
                            select: function(e, ui) {
                                $("#type_of_company2").val(ui.item.type_of_company);
                                $("#unicode2").val(ui.item.unicode);
                                $("#category2").val(ui.item.category);
                                $("#company_address2").val(ui.item.company_address);
                                $("#company_building_name2").val(ui.item.company_building_name);
                                $("input[type=radio][name=company_send_publication2][value=" + ui.item.company_send_publication + "]").attr("checked", "checked");
                                $("#company_website2").val(ui.item.company_website);
                            },
                                        minLength: 1
                         	});
                    }
                    $("#type_of_company").val(v.type_of_company);
                    if (v.type_of_company1 != "" && v.company_name1 != "0") {
                        $.post("/client/type_of_company", {
                            i: 1,
                            s: v.company_name1
                        },
                        function(result) {
                            if (result != "") {
                                $('#company_type').append(result);
                            }
                        });
                    }
                    if (v.type_of_company2 != "" && v.company_name2 != "0") {
                        $.post("/client/type_of_company", {
                            i: 2,
                            s: v.company_name2
                        },
                        function(result) {
                            if (result != "") {
                                $('#company_type').append(result);
                            }
                        });
                    }
                    $("#unicode").val(v.unicode);
                    if (v.unicode1 != "" && v.unicode1 != "0") {
                        $("#company_id").append("<li id='unicode_1' class='13'><input type='text' name='unicode1' onblur=checkunicode('unicode1') id='unicode1' value='" + v.unicode1 + "' style='width:148px;'/></li>");
                    }
                    if (v.unicode2 != "" && v.unicode2 != "0") {
                        $("#company_id").append("<li id='unicode_2' class='13'><input type='text' name='unicode2' onblur=checkunicode('unicode2') id='unicode2' value='" + v.unicode2 + "' style='width:148px;'/></li>");
                    }
                    $("#department").val(v.department);
                    if (v.department1 != "" && v.department1 != "0") {
                        $("#dept").append("<li id='department_1' class='13'><input type='text' id='department1' name='department1' value='" + v.department1 + "' /></li>");
                    }
                    if (v.department2 != "" && v.department2 != "0") {
                        $("#dept").append("<li id='department_2' class='13'><input type='text' id='department2' name='department2' value='" + v.department2 + "' /></li>");
                    }


                    $("#title").val(v.title);
                    if (v.title1 != "" && v.title1 != "0") {
                        $.post("/client/title", {
                            i: 1,
                            s: v.title1
                        },
                        function(result) {
                            if (result != "") {
                                $('#zhichen').append(result);
                            }
                        });
                    }
                    if (v.title2 != "" && v.title2 != "0") {
                        $.post("/client/title", {
                            i: 2,
                            s: v.title2
                        },
                        function(result) {
                            if (result != "") {
                                $('#zhichen').append(result);
                            }
                        });
                    }

                    $("#category").val(v.category);

                    if (v.category1 != "" && v.category1 != "0") {

                        $.post("/client/category", {
                            i: 1,
                            s: v.category1
                        },
                        function(result) {
                            if (result != "") {

                                $('#company_type2').append(result);

                                $("#category1")
                                        .bind('click', function() {
                                            $(this).autocomplete("search");
                                        })
                                        .autocomplete({
                                            minLength: 0,
                                            source: function(request, response) {
                                                                $.ajax({
                                                                        url: "/client/category_josn",
                                                                        dataType: "json",
                                                                        data: {
                                                        query: request.term
                                                                        },
                                                                        success: function(data) {
                                                                                response($.map(data, function(item) {
                                                                                    return {
                                                                label: item.value,
                                                                value: item.value
                                                                                    }
                                                                                }));
                                                                        }
                                                                });
                                                },
                                            focus: function() {
                                                return false;
                                            }

                                        });
                            }
                        });
                    }

                    if (v.category2 != "" && v.category2 != "0") {

                        $.post("/client/category", {
                            i: 2,
                            s: v.category2
                        },
                        function(result) {
                            if (result != "") {
                                $('#company_type2').append(result);

                                $("#category2")
                                        .bind('click', function() {
                                            $(this).autocomplete("search");
                                        })
                                        .autocomplete({
                                            minLength: 0,
                                            source: function(request, response) {
                                                                $.ajax({
                                                                        url: "/client/category_josn",
                                                                        dataType: "json",
                                                                        data: {
                                                        query: request.term
                                                                        },
                                                                        success: function(data) {
                                                                                response($.map(data, function(item) {
                                                                                    return {
                                                                label: item.value,
                                                                value: item.value
                                                                                    }
                                                                                }));
                                                                        }
                                                                });
                                                },
                                            focus: function() {
                                                return false;
                                            }

                                        });


                            }
                        });
                    }
                    $("#company_address").val(v.company_address);
                    if (v.company_address1 != "" && v.company_address1 != "0") {
                        $('#company_addr').append("<li id='company_address_1'><input type='text' id='company_address1' class='large_input' value='" + v.company_address1 + "' name='company_address1' onclick=show_address('company_address1')></li>");
                    }
                    if (v.company_address2 != "" && v.company_address2 != "0") {
                        $('#company_addr').append("<li id='company_address_2'><input type='text' id='company_address2' class='large_input' value='" + v.company_address2 + "' name='company_address2' onclick=show_address('company_address2')></li>");
                    }
                    $("#company_building_name").val(v.company_building_name);
                    if (v.company_building_name1 != "" && v.company_building_name1 != "0") {
                        $.post("/client/company_building_name", {
                            i: 1,
                            s: v.company_building_name1
                        },
                        function(result) {
                            if (result != "") {
                                $('#building_company_name').append(result);
                            }
                        });
                    }
                    if (v.company_building_name2 != "" && v.company_building_name2 != "0") {
                        $.post("/client/company_building_name", {
                            i: 2,
                            s: v.company_building_name2
                        },
                        function(result) {
                            if (result != "") {
                                $('#building_company_name').append(result);
                            }
                        });
                    }
                    $("input[type=radio][name=company_send_publication][value=" + v.company_send_publication + "]").attr("checked", "checked");
                    if (v.company_send_publication1 != "" && v.company_send_publication1 != "0") {
                        $('#per_com').append("<li id='company_send_publication_1'><input type='radio' checked value='Y' id='company_send_publication1' name='company_send_publication1'>Y&nbsp;&nbsp;<input type='radio' value='N' id='company_send_publication1' name='company_send_publication1'>N</li>");
                        $("input[type=radio][name=company_send_publication1][value=" + v.company_send_publication1 + "]").attr("checked", "checked");
                    }
                    if (v.company_send_publication2 != "" && v.company_send_publication2 != "0") {
                        $('#per_com').append("<li id='company_send_publication_2'><input type='radio' checked value='Y' id='company_send_publication2' name='company_send_publication2'>Y&nbsp;&nbsp;<input type='radio' value='N' id='company_send_publication2' name='company_send_publication2'>N</li>");
                        $("input[type=radio][name=company_send_publication2][value=" + v.company_send_publication2 + "]").attr("checked", "checked");
                    }
                    $("#company_website").val(v.company_website);
                    if (v.company_website1 != "" && v.company_website1 != "0") {
                        $('#company_website0').append("<li id='company_website_1'><input type='text' id='company_website1' style='width:374px;' name='company_website1' value='" + v.company_website1 + "'><input type='button' class='input_background' onclick=open_website('company_website1'); value='開啟IE'></li>");
                    }
                    if (v.company_website2 != "" && v.company_website2 != "0") {
                        $('#company_website0').append("<li id='company_website_2'><input type='text' id='company_website2' style='width:374px;' name='company_website2' value='" + v.company_website2 + "'><input type='button' class='input_background' onclick=open_website('company_website2'); value='開啟IE'></li>");
                    }
                    $("#front_man").val(v.front_man);
                    $("#head").val(v.head);
                    $("#original_broker").val(v.original_broker);
                    $("#broker").val(v.broker);
                    $("#birthday").val(v.birthday);
                    $("#gifts").val(v.gifts);
                    $("#id_number").val(v.id_number);
                    $("#usually_contact").val(v.usually_contact);
                    if (v.usually_contact1 != "" && v.usually_contact1 != "0") {
                        add_usually_contact();
                        $("#usually_contact1").val(v.usually_contact1);
                    }
                    if (v.usually_contact2 != "" && v.usually_contact2 != "0") {
                        add_usually_contact();
                        $("#usually_contact2").val(v.usually_contact2);
                    }
                    $("#usually_start_time").val(v.usually_start_time);
                    if (v.cusually_start_time1 != "" && v.cusually_start_time1 != "0") {
                        $("#usually_start_time1").val(v.usually_start_time1);
                    }
                    if (v.cusually_start_time2 != "" && v.cusually_start_time2 != "0") {
                        $("#usually_start_time2").val(v.usually_start_time2);
                    }
                    $("#usually_end_time").val(v.usually_end_time);
                    if (v.usually_end_time1 != "" && v.usually_end_time1 != "0") {
                        $("#usually_end_time1").val(v.usually_end_time1);
                    }
                    if (v.usually_end_time2 != "" && v.usually_end_time2 != "0") {
                        $("#usually_end_time2").val(v.usually_end_time2);
                    }
                    $("#holidays_contact").val(v.holidays_contact);
                    if (v.holidays_contact1 != "" && v.holidays_contact1 != "0") {
                        add_holidays_contact();
                        $("#holidays_contact1").val(v.holidays_contact1);
                    }
                    if (v.holidays_contact2 != "" && v.holidays_contact2 != "0") {
                        add_holidays_contact();
                        $("#holidays_contact2").val(v.holidays_contact2);
                    }
                    $("#holidays_start_time").val(v.holidays_start_time);
                    if (v.holidays_start_time1 != "" && v.holidays_start_time1 != "0") {
                        $("#holidays_start_time1").val(v.holidays_start_time1);
                    }
                    if (v.holidays_start_time2 != "" && v.holidays_start_time2 != "0") {
                        $("#holidays_start_time2").val(v.holidays_start_time2);
                    }
                    $("#holidays_end_time").val(v.holidays_end_time);
                    if (v.holidays_end_time1 != "" && v.holidays_end_time1 != "0") {
                        $("#holidays_end_time1").val(v.holidays_end_time1);
                    }
                    if (v.holidays_end_time2 != "" && v.holidays_end_time2 != "0") {
                        $("#holidays_end_time2").val(v.holidays_end_time2);
                    }
                    $("#background_and_features").val(v.background_and_features);
                }
            }
        }, "json");


    } else {
        alert("請填寫客戶姓名");
        setTimeout(function() {
            $("#name").focus();
        }, 0);
    }
}

function detail_info() {
    if ($("#detail_info").css("display") == "none") {
        $("#detail_info").show();
    } else {
        $("#detail_info").hide();
    }
}

function detail_info_determine() {
    $("#detail_info").hide();
}